package com.drug.mingkang.service.impl;

import com.drug.mingkang.model.*;
import com.drug.mingkang.mapper.HqPdetailMapper;
import com.drug.mingkang.service.*;
import com.drug.mingkang.utils.ResponseMessage;
import net.bytebuddy.asm.Advice;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 总公司计划明细(HqPdetail)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@Service("hqPdetailService")
public class HqPdetailServiceImpl implements HqPdetailService {
    @Resource
    private HqPdetailMapper hqPdetailMapper;

    @Resource
    private HqPlanService hqPlanService;

    @Resource
    private HqDrugmaterialsService hqDrugmaterialsService;

    @Resource
    private HqRawmaterialService rawmaterialService;
    /**
     * 通过生产计划ID查询多条数据
     *
     * @param planId 主键
     * @return 实例对象
     */
    @Override
    public List<HqPdetail> queryById(String planId) {

        return this.hqPdetailMapper.queryByPlanId(planId);
    }


    /**
     * 根据药品和计划单状态查询计划详情
     * @param drugId
     * @return
     */
    @Override
    public List<HqPdetail> selectByState(String[] drugId) {
        return this.hqPdetailMapper.selectByState(drugId);
    }


    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<HqPdetail> queryAllByLimit(int offset, int limit) {
        return this.hqPdetailMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param hqPdetail 实例对象
     * @return 实例对象
     */
    @Override
    public HqPdetail insert(HqPdetail hqPdetail) {
        this.hqPdetailMapper.insert(hqPdetail);
        return hqPdetail;
    }


    /**
     * 批量增加生产计划明细
     * @param list
     * @return
     */
    @Override
    public ResponseMessage insertAll(List<HqPdetail> list) {

        ResponseMessage responseMessage = new ResponseMessage();
        List<String> drugNames = new ArrayList<>();
        HqPlan hqPlan = new HqPlan();
        hqPlan.setPlanId(list.get(0).getPlanId());

        //生产计划金额
        double planPrice = 0;

        for(int i=0;i<list.size();i++){
            int num = list.get(i).getNum();
            double pdetailPrice = 0;
            //根据生产计划明细中的药品id查找药品的bom表信息
            List<HqDrugmaterials> hqDrugmaterials = this.hqDrugmaterialsService.queryByDrugId(list.get(i).getDrugId());
            //如果没有则返回-1
            if(hqDrugmaterials.size()<1){
                drugNames.add(list.get(i).getDrugName());
            }else{
                for (HqDrugmaterials drugmaterials:hqDrugmaterials) {
                    Material material = this.rawmaterialService.queryById(drugmaterials.getMaterialId());
                    pdetailPrice+=material.getMoney()*num;
                }
                planPrice += pdetailPrice;
                list.get(i).setPrice(pdetailPrice);
            }

        }
        if(drugNames.size()>0){
            responseMessage =  ResponseMessage.error("没有药品bom",drugNames);
        }else{
            hqPlan.setPlanPrice(planPrice);
            int row = this.hqPlanService.update(hqPlan);
            hqPdetailMapper.deleteById(list.get(0).getPlanId());
            responseMessage = this.hqPdetailMapper.insertBatch(list)>0?ResponseMessage.success():ResponseMessage.error();

        }

        return responseMessage;
    }

    /**
     * 修改数据
     *
     * @param hqPdetail 实例对象
     * @return 实例对象
     */
    @Override
    public HqPdetail update(HqPdetail hqPdetail) {
        this.hqPdetailMapper.update(hqPdetail);
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param pdetailId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String pdetailId) {
        return this.hqPdetailMapper.deleteById(pdetailId) > 0;
    }
}